<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),	
	);
	$list=$this->get_list('agent',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['pcd']=json_decode($item['pcd'],true);
			$item['pcdinfo']=$item['pcd']['province'].' '.$item['pcd']['city'].' '.$item['pcd']['district'].' '.$item['pcd']['street'];
			$item['order_num']=$this->count('order',array('dian_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['order_price']=$this->getsum('order',array('dian_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'order_price');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'自提点名称','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'level_id','title'=>'等级','type'=>'select','options'=>'levels'),
			array('name'=>'pcdinfo','title'=>'所属地区','type'=>'string'),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'status','title'=>'自提点状态','type'=>'radio','options'=>'agent_status'),
			array('name'=>'order_num','title'=>'自提点订单数','type'=>'string'),
			array('name'=>'order_price','title'=>'自提点订单总金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'agent_status'),
			array('type'=>'choose','width'=>18,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['agent_status']=$this->agent_status;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('agent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('city'=>'');
		$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array('lat'=>'');
		$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	}else{
		$item=array(
			'title'=>'',
		);
	}
   
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'自提点设置',
		'edits'=>array(array('title'=>'基本信息','list'=>array(
			array('name'=>'title','title'=>'自提点名称','type'=>'string'),
			array('name'=>'level_id','title'=>'等级','type'=>'select','options'=>'levels'),
			array('name'=>'pcd','title'=>'自提点区域','type'=>'pcd'),
			array('name'=>'lat_lng','title'=>'自提点坐标','type'=>'lat_lng'),
			array('name'=>'name','title'=>'联系电话','type'=>'string'),
			array('name'=>'mobile','title'=>'联系人','type'=>'string'),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'codeimg','title'=>'门头照片','type'=>'images'),
			array('type'=>'choose','name'=>'mid','title'=>'绑定会员','action'=>'core/member.get_member/'),
		))),
	);
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	$return['item']=$item;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'name'=>trim($postdata['name']),
		'mobile'=>trim($postdata['mobile']),
		'address'=>trim($postdata['address']),
		'mid'=>intval($postdata['mid']),
		'level_id'=>intval($postdata['level_id']),
		'pcd'=>$postdata['pcd']?ijson_encode($postdata['pcd']):'',
		'lat_lng'=>$postdata['lat_lng']?ijson_encode($postdata['lat_lng']):'',
	);
	$id=intval($postdata['id']);
	if($id){
		$this->update('agent',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$this->insert('agent',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('member')->check_agent($apply_id,$status,$post['remark']);
		exi('审核成功！','','list');
	}
	$item=$this->get('agent',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定自提点不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核自提点申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'agent_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['agent_status']=$this->agent_status;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('agent',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('agent',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	$list=$this->getall('agent',$params);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['nickname']=$item['member']['nickname'];
			$item['pcd']=json_decode($item['pcd'],true);
			$item['pcdinfo']=$item['pcd']['province'].' '.$item['pcd']['city'].' '.$item['pcd']['district'].' '.$item['pcd']['street'];
			$item['status']=$this->agent_status[$item['status']];
		}
	}
	$header=array(
		'title'=>'自提点名称',
		'nickname'=>'会员信息',
		'pcdinfo'=>'所属地区',
		'address'=>'详细地址',
		'status'=>'自提点状态',
		'order_num'=>'自提点订单数',
		'order_price'=>'自提点订单总金额',
	);
	cfc('tools')->export2excel($header,$list,'自提点列表');
}
?>